package com.chenglu.android.mine.login.mvp.presenter;




import android.accounts.NetworkErrorException;

import com.chenglu.android.bean.LoginBean;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.login.mvp.contract.LoginContract;
import com.chenglu.android.mine.login.mvp.model.LoginModel;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.LoginPresenter {

     private  LoginContract.LoginModel model;

    public LoginPresenter(){
        model = new LoginModel();
    }


    @Override
    public void getLogin(String username, String smscode,String grantType) {
        if(!isViewAttached()){
            return;
        }
        model.getLogin(username, smscode,grantType)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<LoginBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull LoginBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getLogin(bean);
                        }else {
                            mView.getLogin(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void refreshTokenLogin(String grantType, String refresh_token) {

    }

    @Override
    public void getSmsCode(String mobile) {
        if(!isViewAttached()){
            return;
        }
        model.getSmsCode(mobile)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<SmsCodeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull SmsCodeBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getSmsCode(bean);
                        }else if(bean.getCode()==(Constant.HTTPCODE400)) {
//                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE401)) {
//                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE404)) {
//                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE500)) {
//                            ToastUtils.show(bean.getMsg());

                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        //记得上线删除
//                        ToastUtils.show(e.getMessage());
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getOneLogin(String accessToken, String grantType) {
        if(!isViewAttached()){
            return;
        }
        model.getOneLogin(accessToken,grantType)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<LoginBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull LoginBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getOneLogin(bean);
                        }else {
                            mView.getOneLogin(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }
}
